<template>
  <div class="login-container">
    <!-- 登录导航 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="arrow-left" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录表单验证 -->
    <van-form @submit="onSubmit" ref="loginFrom">
      <van-field
        v-model="user.mobile"
        autocomplete="off"
        placeholder="请输入手机号"
        name="mobile"
        :rules="userFromRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        autocomplete="off"
        name="code"
        placeholder="请输入验证码"
        :rules="userFromRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <!-- 验证码按钮 -->
        <template #button>
          <van-count-down v-if="isshow" :time="1000*6" format="ss 秒" @finish="isshow=false" />
          <van-button v-else class="send-sms-btn" round size="small" type="default"
          native-type="button"
          @click="yambtn"
          >发送验证码</van-button>
        </template>
        <!-- 验证码按钮 -->
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login,sendSms } from "../../api/user.js";
export default {
  data() {
    return {
      user: {
        mobile: "",
        code: "",
      },
      userFromRules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空",
          },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: "手机号格式错误",
          }
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空",
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误",
          },
        ],
      },
      isshow:false,
    };
  },
  methods: {
    async onSubmit() {
      //获取用户输入的手机号和验证码
      const user = this.user;

    /*   我们的登录功能增加 toast 交互提示 */
      this.$toast.loading({
        message: "加载中...", // 提示消息
        forbidClick: true, // 是否禁止背景点击
        duration: 0, // 持续时间，0表示持续展示不停止
      });


      try {
        const res = await login(user);
        this.$toast.success("登录成功");  //在次调用话会停止loading方法 提示加载中 改为登录成功
       // 登录成功以后，跳转到原来页面
       // back 的方式不严谨，后面我们会进行优化
       
        console.log(res);
        //调用mutations里的方法把数据传到vuex数据储存中
        this.$store.commit('setUser',res.data.data)
        this.$router.back()
      } catch (err) {
        if (err.response && err.response.status === 400) {
          this.$toast.fail("手机号或验证码错误");
        } else {
          this.$toast.fail("登录失败");
        }
      }
    },
    async yambtn(){
      try {
    await this.$refs.loginFrom.validate('mobile')
      }catch (err) {
   return console.log('验证错误',err);
      }
      this.isshow = true
      /* 发送验证码请求 */
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        this.isshow =false
        if(err.response && err.response.status ===429){
          this.$toast('发送太频繁了')
        }else{
          this.$toast('发送失败')
        }
      }
    }
  },
};
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>